﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.Composition;
using MEF_Test.Bricks.TemplateProvider;
using ShareBrowserAction;
using System.Windows;
using MEF_Test.Contracts;
using MEF_Test.Bricks;


namespace ShareAction
{

    [Export(typeof(INodeAction))]
    // Important otherwise, only one instance of the ShareAction will ever be generated, and all the items cannot use the same instance of the action :
    // especially for the Presenter, because a visual cannot be more than once in the visual tree !
    [PartCreationPolicy(CreationPolicy.Shared)]
    [ExportMetadata("Action", "Share")]
    public class ShareAction : NodeActionBase
    {
        
        private readonly IEnumerable<Lazy<IShareActionContextProvider>> _shareProviders;
        private readonly TemplateProvider _templateProvider;

        [ImportingConstructor]
        public ShareAction([ImportMany(typeof(IShareActionContextProvider))] IEnumerable<Lazy<IShareActionContextProvider>> shareProviders, TemplateProvider templateProvider)
        {
            _shareProviders = shareProviders;
            _templateProvider = templateProvider;
        }

        private void Share(IShareActionContextProvider shareActionProvider, IBrowserNodeViewModel node)
        {
            shareActionProvider.Share(node);  
          
        }

        protected override object onExecuteActivateCommand(IBrowserNodeViewModel node)
        {
            Share(_shareProviders.ElementAt(0).Value, node);
            return true;
        }
      
        #region INodeAction Members

        /// <summary>
        /// Gets the template.
        /// </summary>
        /// <value>The template.</value>
        public override DataTemplate Template
        {
            get
            {
                throw new NotImplementedException("This Template property doesn't have any sense : please, correct this to reflect the same behavior as in the OpenAction.");
                var source = new Uri("/ShareAction;component/Resources.xaml", UriKind.Relative);
                var resourceDictionary = new ResourceDictionary { Source = source };
                var dt = (DataTemplate)resourceDictionary["ShareActionTemplate"];

                // in this example : we will use the ShareActionTemplate in the Resource.xaml file only if no type was registered to be used with button.Action.Share to override it !
                return _templateProvider.GetTemplate("Button.Action.Share");
            }
        }

        #endregion

        /// <summary>
        /// Gets the compatible providers.
        /// </summary>
        /// <param name="browserNodeBase">The browser node base.</param>
        /// <returns></returns>
        public override IEnumerable<IActionContextProvider> GetCompatibleProviders(IBrowserNodeViewModel browserNodeBase)
        {
            var compatibleProviders = from provider in _shareProviders
                                      where provider.Value.Supports(browserNodeBase)
                                      select provider.Value;
            
            return compatibleProviders;
        }
    }
}
